﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;

namespace School.UserCentents
{
    public partial class School : UserCheckPower
    {
        #region 传递参数
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get
            {
                if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["id"].ToString());
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.School Sch = new BLL.School();
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel p1 = (Panel)Master.FindControl("Panel1");
            Panel p2 = (Panel)Master.FindControl("Panel2");
            Panel p3 = (Panel)Master.FindControl("Panel3");
            Panel p4 = (Panel)Master.FindControl("Panel4");
            p1.CssClass = "sldmenuoff";
            p2.CssClass = "sldmenuon0";
            p3.CssClass = "sld1";
            p4.CssClass = "sld0";
            p3.Attributes.Add("style", "display: none");
            p4.Attributes.Add("style", "");

            if (!IsPostBack)
            {
                if (ID > 0)
                {
                    Model.School s = Sch.GetModel(ID);
                    txtName.Text = s.Name;
                    txtJianjie.Text = s.Contents;
                    txtGongGao.Text = s.GongGao;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ID > 0)
            {
                Model.School s = Sch.GetModel(ID);
                if (s != null)
                {
                    s.Name = txtName.Text;
                    s.Contents = txtJianjie.Text;
                    s.GongGao = txtGongGao.Text;
                    Sch.Update(s);
                    ScriptHelper.AlertRedirect("修改成功！", "AddSchool.aspx?id=" + ID);
                }
            }
            else
            {
                Model.School s = new Model.School();

                Model.Users u = Users.GetModel(LoginUserId);
                DataSet ds = Sch.GetList("CreateUserID=" + LoginUserId);
                if (ds.Tables[0].Rows.Count >= u.XiaoQuCount)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('创建学校已到最大数，请联系您的上级！');</script>", false);
                    return;
                }
                s.CreateUserID = LoginUserId;
                s.CreateTime = DateTime.Now;
                s.Name = txtName.Text;
                s.Contents = txtJianjie.Text;
                s.GongGao = txtGongGao.Text;
                Sch.Add(s);
                ScriptHelper.AlertRedirect("增加成功！", "School.aspx");
            }
        }
    }
}